package com.kfd.demo.service.Impl;

import com.kfd.demo.dao.TheMunuDao;
import com.kfd.demo.entity.menu.Left_Menu_Tb;
import com.kfd.demo.entity.menu.Menu_Tb;
import com.kfd.demo.entity.menu.Right_Menu_Data;
import com.kfd.demo.entity.menu.Right_Menu_Tb;
import com.kfd.demo.service.TheMunuServe;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TheMunServeImpl implements TheMunuServe {

    @Autowired
    TheMunuDao theMunuDao;

    @Override
    public Map<String, Object> getRightMenuData() {
        List MenuList = new ArrayList(); // 一级菜单表
        List MenuList_Data_ = theMunuDao.getMenuTb(); // 一级菜单数据

// 创建一级菜单的容器装载一级菜单
        Map<String, Object> MenuTb = new HashMap<String, Object>();

// 遍历一级菜单数据
        for (Object menu_tb_ : MenuList_Data_) {
            Menu_Tb Menu_Data = (Menu_Tb) menu_tb_;
            MenuList.add(Menu_Data);

            // 创建一个新的 LeftMenuTb 列表来装载二级菜单
            List LeftMenuTb = new ArrayList<>();
            List LeftMenuTb_Data = theMunuDao.getLeftMenuTb(Menu_Data.getMenu_id());
            // 遍历二级菜单数据
            for (Object leftmenu_ : LeftMenuTb_Data) {
                Left_Menu_Tb leftMenuData = (Left_Menu_Tb) leftmenu_;
                LeftMenuTb.add(leftMenuData);

                //创建一个右侧的三级菜单List
                List RightMenuTb = new ArrayList<>();
                List RightMenuTb_Data = theMunuDao.getRightMenuTb(
                        Menu_Data.getMenu_id(),
                        leftMenuData.getLeft_menu_id(),
                        leftMenuData.getLeft_menu_id()
                );
                for (Object rightmenu_ : RightMenuTb_Data) {
                    Right_Menu_Tb rightMenuData = (Right_Menu_Tb) rightmenu_;
                    RightMenuTb.add(rightMenuData);

                    List RightMenuData = new ArrayList<>();
                    List RightMenuTb_DataChldren = theMunuDao.getRightMenuData(
                            Menu_Data.getMenu_id(),
                            leftMenuData.getLeft_menu_id(),
                            rightMenuData.getLeft_menu_id(),
                            rightMenuData.getRight_menu_id(),
                                rightMenuData.getRight_menu_id()
                    );
                    for(Object rightmenuchildren_ : RightMenuTb_DataChldren){
                        Right_Menu_Data rightMenuDataChildren = (Right_Menu_Data) rightmenuchildren_;
                        RightMenuData.add(rightMenuDataChildren);

                    }
                    rightMenuData.setChildren(RightMenuData);

                }
                leftMenuData.setChildren(RightMenuTb);// 二级装崽

                //                List

            }
            Menu_Data.setChildren(LeftMenuTb);// 一级装崽

//            System.out.println("这里是一级菜单的内容" + Menu_Data + "\n");
            // 将一级菜单放入 MenuTb 中
            MenuTb.put("MenuTb", MenuList);
        }
        return MenuTb;
    }
}
